package com.droidathletics.boundary.competition;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import com.droidathletics.R;
import com.droidathletics.boundary.MainMenu;
import com.droidathletics.control.ManageCompetition;
import com.droidathletics.entity.Date;

public class CreateCompetition  extends Activity {
	private Button saveButton;
	private Button pickDateButton;
	private ManageCompetition manageCompetition;
    private EditText name;
    private EditText place;
    private EditText date;
    private EditText participants;
    private TextView errorData;
    private int dateYear,dateMonth,dateDay;
    
    static final int DATE_DIALOG_ID = 0;

 // the callback received when the user "sets" the date in the dialog
    private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) {
                    dateYear = year;
                    dateMonth = monthOfYear + 1;
                    dateDay = dayOfMonth;
                    updateField();
                }
            };
            
    private void updateField(){
    	date.setText(new StringBuilder()
	        .append(dateMonth + 1).append("/")
	        .append(dateDay).append("/")
	        .append(dateYear).append(" "));
    }
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_competition);
        
        manageCompetition = MainMenu.manageCompetition;
        
        name = (EditText) findViewById(R.id.fieldCompetitionName);
        place = (EditText) findViewById(R.id.fieldCompetitionPlace);
        date = (EditText) findViewById(R.id.fieldCompetitionDate);
        participants = (EditText) findViewById(R.id.fieldCompetitionNumber);
        
        errorData = (TextView) findViewById(R.id.labelErrorDataCompetition);
        
        saveButton = (Button) findViewById(R.id.createCompetitionSave);
        pickDateButton = (Button)findViewById(R.id.buttonCCompetitionPickDate); 
        
        pickDateButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });
        saveButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Date date = new Date(dateDay, dateMonth, dateYear);
            	String errorResult = manageCompetition.createCompetition(
            			(String)name.getText().toString(), 
            			String.valueOf(dateDay), 
            			String.valueOf(dateMonth), 
            			String.valueOf(dateYear), 
            			(String)place.getText().toString(), 
            			(String)participants.getText().toString());
            	
            	if ( errorResult == "")
            	{
            		finish();
                	Intent intent = new Intent(CreateCompetition.this, MainMenu.class);
                    startActivity(intent);
            	}
            	else
            	{
            		errorData.setText(errorResult);
            	}

            }
        });
     // get the current date
        final Calendar c = Calendar.getInstance();
        dateYear = c.get(Calendar.YEAR);
        dateMonth = c.get(Calendar.MONTH);
        dateDay = c.get(Calendar.DAY_OF_MONTH);
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
	    switch (id) {
	    case DATE_DIALOG_ID:
	        return new DatePickerDialog(this,
	                    mDateSetListener,
	                    dateYear, dateMonth, dateDay);
	    }
	    return null;
	}
	
	@Override
    public void onBackPressed() {
		finish();
        Intent intent = new Intent(CreateCompetition.this, MainMenu.class);
        startActivity(intent);
	    return;
    }

}

